from django.shortcuts import render
from rest_framework.response import Response

from goods.models import SKU
from utils.pagination import StandardResultsSetPagination
"""
所谓的静态化 其实就是 让用户 直接去访问 我们处理好的 html页面

问题:
    我们首页的数据 会发生变化


1. 先查询数据库的数据
2. 通过模板来渲染数据
3. 模板渲染好的html 写入到指定 的文件目录

"""
# Create your views here.
"""
用户选择不同的分类,我们返回响应的热销商品,返回2个热销商品

1.接收分类id
2.根据分类id获取商品信息, [SKU,SKU]
3.进行序列化操作(将对象转换为字典,json)
4.返回响应

GET     /goods/categories/(?P<category_id>\d+)/hotskus/
"""
# APIView
# GenericAPIView
# ListAPIView,RetriveAPIView
from rest_framework.views import APIView
from .serializers import HotSKUSerializer

# class HotSKUView(APIView):
#
#     def get(self,request,category_id):
#         # 1.接收分类id
#         # 2.根据分类id获取商品信息, [SKU,SKU]
#
#         # 分类
#         # category 相当于 一个实例对象  category = 对象
#         # 如果你想给 category 赋值一个 id
#         # 这个时候 你只能 使用 数据表中的字段名 = 值
#         # category_id = 1
#         #category = GoodCategory.objects.get(category_id=category_id)
#
#         # 上架
#         # where category_id=xxx and is_lanuched=True
#
#         skus = SKU.objects.filter(is_launched=True,category_id=category_id).order_by('-sales')[:2]
#         # skus = SKU.objects.filter(is_launched=True,category_id=category_id)
#         # 3.进行序列化操作(将对象转换为字典,json)
#         serializer = HotSKUSerializer(skus,many=True)
#         # 4.返回响应
#         return Response(serializer.data)


from rest_framework.generics import ListAPIView
class HotSKUView(ListAPIView):

    pagination_class = None

    serializer_class = HotSKUSerializer

    # queryset = SKU.objects.filter(is_launched=True,category_id=category_id).order_by('-sales')[:2]

    def get_queryset(self):
        category_id = self.kwargs['category_id']
        return SKU.objects.filter(is_launched=True,category_id=category_id).order_by('-sales')[:2]


from rest_framework.filters import OrderingFilter
from rest_framework.pagination import PageNumberPagination
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView

class SKUAPIView(ListAPIView):
    """

    1. 根据分类获取所有数据
    2. 排序
    3. 分页

    GET     /goods/categories/(?P<category_id>\d+)/skus/?ordering=-price&page=2&page_size=2

    """

    #排序实现
    filter_backends = [OrderingFilter]
    ordering_fields = ['price','sales','create_time']

    #分页
    pagination_class = StandardResultsSetPagination

    serializer_class = HotSKUSerializer

    # queryset = SKU.objects.filter(is_launched=True,category_id=category_id)

    def get_queryset(self):
        category_id = self.kwargs['category_id']
        return SKU.objects.filter(is_launched=True,category_id=category_id)

"""
 id=100       我是中国人。

我       100
中国      100
人       100
中国人     100
我是中国    100


apple       1,2,3,4,5,6,7,8



搜索的步骤分为 2步:

一  elasticsearch 帮助我们实现搜索引擎的功能
     我们通过docker的方式让它运行起来

二  haystack 去连接 elasticsearch
    ① 配置 haystack
    ② 定义搜索索引类,让elasticsearch 去分词来创建 全文索引
    ③ 搜索


"""
from .serializers import SKUIndexSerializer
from drf_haystack.viewsets import HaystackViewSet

class SKUSearchViewSet(HaystackViewSet):
    """
    SKU搜索
    """
    index_models = [SKU]

    serializer_class = SKUIndexSerializer





























































